package com.shigu.user.action;

import com.shigu.MallResponse;
import com.shigu.PageResponse;
import com.shigu.TokenRequest;
import com.shigu.address.bo.*;
import com.shigu.address.service.AddressAdminService;
import com.shigu.address.service.AddressService;
import com.shigu.address.vo.AddressDetailResponse;
import com.shigu.address.vo.AddressResponse;
import com.shigu.component.enums.SessionEnum;
import com.shigu.exceptions.WapException;
import com.shigu.user.service.MallTokenService;
import com.shigu.weixin.vo.WxAppUser;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.annotation.ApplicationScope;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@Controller
@RequestMapping("wapsjson")
public class AddressController {
    @Autowired
    AddressAdminService addressAdminService;
    @Autowired
    AddressService addressService;
    @Autowired
    MallTokenService mallTokenService;

    @RequestMapping("addressList")
    @ResponseBody
    public PageResponse<AddressResponse> addressList(TokenRequest request) {
        return addressService.addressList(mallTokenService.selUserIdByToken(request.getToken()));
    }

    @RequestMapping("delAddress")
    @ResponseBody
    public MallResponse delAddress(@Valid AddressRequest request, BindingResult result) throws WapException {
        if(result.hasErrors()){
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        addressAdminService.delAddress(mallTokenService.selUserIdByToken(request.getToken()),request.getAddressId());
        return new MallResponse().success();
    }

    @RequestMapping("updateAddress")
    @ResponseBody
    public MallResponse updateAddress(@Valid UpdateAddressRequest request, BindingResult result) throws WapException {
        if(result.hasErrors()){
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        ModUserAddressBo bo = new ModUserAddressBo();
        bo.setAddressId(request.getAddressId());
        bo.setProvId(request.getProvId());
        bo.setAreaId(request.getAreaId());
        bo.setCityId(request.getCityId());
        bo.setImDefault(request.isImDefault());
        bo.setReceiveAddress(request.getReceiveAddress());
        bo.setReceiveName(request.getReceiveName());
        bo.setReceiveTelephone(request.getReceiveTelephone());

        addressAdminService.updateAddress( mallTokenService.selUserIdByToken(request.getToken()),bo);
        return new MallResponse().success();
    }

    @RequestMapping("setDefaultAddress")
    @ResponseBody
    public MallResponse setDefaultAddress(@Valid AddressRequest request, BindingResult result) throws WapException{
        if(result.hasErrors()){
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        addressAdminService.setDefaultAddress( mallTokenService.selUserIdByToken(request.getToken()),request.getAddressId());
        return new MallResponse().success();
    }

    @RequestMapping("addAddress")
    @ResponseBody
    public MallResponse addAddress(@Valid AddAddressRequest request, BindingResult result, HttpSession session) throws WapException{
        if(result.hasErrors()){
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        UserAddressBo bo = new UserAddressBo();
        bo.setProvId(request.getProvId());
        bo.setAreaId(request.getAreaId());
        bo.setCityId(request.getCityId());
        bo.setImDefault(request.isImDefault());
        bo.setReceiveAddress(request.getReceiveAddress());
        bo.setReceiveName(request.getReceiveName());
        bo.setReceiveTelephone(request.getReceiveTelephone());

        addressAdminService.addAddress( mallTokenService.selUserIdByToken(request.getToken()),bo);
        return new MallResponse().success();
    }

    @RequestMapping("myDefaultAddress")
    @ResponseBody
    public AddressResponse myDefaultAddress(TokenRequest request) throws WapException {
        return addressService.myDefaultAddress( mallTokenService.selUserIdByToken(request.getToken()));
    }

    @RequestMapping("selAddressById")
    @ResponseBody
    public AddressDetailResponse selAddressById(@Valid AddressRequest request, BindingResult result) throws WapException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        return  addressService.selAddressById(request.getAddressId());
    }
}
